package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivitySearchAllGod;
import nero.yncc.com.createart.activity.ActivitySearchAllType;
import nero.yncc.com.createart.bean.BeanCategory;
import nero.yncc.com.createart.bean.BeanSearchAll;
import nero.yncc.com.createart.bean.BeanType;
import nero.yncc.com.createart.bean.BeanTypeIn;
import nero.yncc.com.createart.bean.SearchInBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;

/**
 * Created by nero on 17-6-26.
 */

public class AdapterTypeAll extends RecyclerView.Adapter<AdapterTypeAll.Holder> {
    Context context;
    ActivitySearchAllType activitySearchAllType;
    ArrayList<BeanCategory> arrayList;

    public AdapterTypeAll(Context context, ActivitySearchAllType activitySearchAllType, ArrayList<BeanCategory> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
        this.activitySearchAllType = activitySearchAllType;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_type_all, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final BeanCategory beanType = arrayList.get(position);

        if (TextUtils.isEmpty(beanType.getChooseName())) {
            holder.tv_title.setText(beanType.getName());
            holder.tv_title.setTextColor(context.getResources().getColor(R.color.textColorGray_deep));
        } else {
            holder.tv_title.setText(beanType.getName());
            holder.tv_title.setTextColor(context.getResources().getColor(R.color.project_yellow));
            holder.rc_list.setVisibility(View.VISIBLE);
            ((ActivitySearchAllType) context).noChoose();
        }

        if(beanType.isOpen()){
            holder.rc_list.setVisibility(View.VISIBLE);
        }else{
            holder.rc_list.setVisibility(View.GONE);
        }
        holder.ll_type_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (beanType.getArrayList().size() <= 0)
                    ((ActivitySearchAllType) context).getCategory(beanType.getId(), beanType.getArrayList());
                if (holder.rc_list.getVisibility() == View.GONE){
                    beanType.setOpen(true);
                    holder.rc_list.setVisibility(View.VISIBLE);
                }else{
                    beanType.setOpen(false);
                    holder.rc_list.setVisibility(View.GONE);
                }

            }
        });


        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        holder.rc_list.setLayoutManager(manager);

        //存放下面选项的arraylist
        final ArrayList<BeanCategory> arrayList = beanType.getArrayList();


        AdapterTypeALlInText adapterSearchALlInText = new AdapterTypeALlInText(context, arrayList, new AdapterSearchAll.DochildrenClick() {
            @Override
            public void click(int position) {
                reset();
                arrayList.get(position).setChoose(true);
                holder.tv_title.setText(arrayList.get(position).getName());
                holder.tv_title.setTextColor(context.getResources().getColor(R.color.project_yellow));
                beanType.setChooseName(arrayList.get(position).getName());
                AdapterTypeAll.this.notifyDataSetChanged();
                Intent data = new Intent();
                data.putExtra("data", arrayList.get(position));
                activitySearchAllType.setResult(200, data);
                activitySearchAllType.finish();
            }
        });
        holder.rc_list.setAdapter(adapterSearchALlInText);
        adapterSearchALlInText.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tv_title;
        IconView icon_right;
        MyRecyclerView rc_list;
        LinearLayout ll_type_title;

        public Holder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            icon_right = itemView.findViewById(R.id.icon_right);
            rc_list = itemView.findViewById(R.id.rc_list);
            ll_type_title = itemView.findViewById(R.id.ll_type_title);
        }
    }

    public void reset() {
        for (BeanCategory bean : arrayList) {
            bean.setChooseName("");
            for (BeanCategory beanin : bean.getArrayList()) {
                beanin.setChoose(false);
            }
        }
    }
}
